package com.xjoyt.pay.share.controllers;

import java.util.List;


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xjoyt.pay.controllers.AbstractController;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.share.interfaces.AreaContract;
import com.xjoyt.pay.share.params.AreaPageParam;
import com.xjoyt.pay.share.po.Area;
import com.xjoyt.pay.share.services.AreaService;

@RestController
@RequestMapping("area")
public class AreaController extends AbstractController implements AreaContract {

	@Autowired
	private AreaService service;

	@Override
	public Area get(@PathVariable("id") String id) {
		return service.get(id);
	}

	@Override
	public PageResult<Area> loadPage(@RequestBody PageParam<AreaPageParam> param) {
		if (StringUtils.isBlank(param.order))
			param.order = "desc";
		return service.loadPage(param);
	}

	@Override
	public ExecuteResult<Area> save(@RequestBody Area param) {
		return service.save(param);
	}

	@Override
	public ExecuteResult<?> remove(@PathVariable("id") String id) {
		return service.remove(id);
	}

	@Override
	public List<Area> findAllByParentId(@PathVariable(value = "parentId", required = false) String parentId) {
		if (StringUtils.isBlank(parentId))
			parentId = "0";
		return service.findAllByParentId(parentId);
	}

	@Override
	public ExecuteResult<?> fetch(@PathVariable("key") String key) {
		try {
			return service.fetch(key);
		} catch (Exception e) {
			e.printStackTrace();
			return ExecuteResult.fail();
		}
	}

	@Override
	public ExecuteResult<?> fetchByParentId(@PathVariable("key") String key,
			@PathVariable(value = "parentId", required = false) String parentId) {
		if (StringUtils.isBlank(parentId))
			parentId = "0";
		try {
			return service.fetch(key, parentId);
		} catch (Exception e) {
			e.printStackTrace();
			return ExecuteResult.fail();
		}
	}

	@Override
	public ExecuteResult<Boolean> isRunFetch() {
		return service.isRunFetch();
	}

	@Override
	public ExecuteResult<?> stopFetch() {
		return service.stopFetch();
	}

}
